layui.extend({
	hxNav: ctx+'/common/layuiExt/hxNav' 
})

layui.use(['hxNav'], function () {
	var element = layui.element;
    //侧边导航
    layui.hxNav({
        element: '#hxNavbar',        
        url: ctx+'/getSideNav',  
        type: 'get',
        shrink: false, 
        onSelect: function(v) {
      	  var text = v.text;
      	  var id = v.id;
      	  var href = v.href;
      	  if(!v.hasChildren){
      		  createTab(text,id,href);
      	  }
        }
        ,autoExpand:true
        //,background: '#ffffff'
    });
    
    /* 侧边栏开关 */
    $(".side-toggle").on("click", function (e) {
        e.preventDefault();
        var to = $(".layui-layout-admin");
        to.toggleClass("layui-side-shrink");
        to.attr("toggle") === 'on' ? to.attr("toggle", "off") : to.attr("toggle", "on");
    });
    $(".layui-side").on("click", function () {
        var to = $(".layui-layout-admin");
        if (to.attr("toggle") === 'on') {
            to.attr("toggle", "off");
            to.removeClass("layui-side-shrink");
        }
    });

    function createTab(text,id,href){
  	  var lis = [];
        $(".layui-tab-title li").each(function () {
            lis.push($(this).attr("lay-id"));
        })
        if (lis.length <= 0 || lis.indexOf(id) == -1) {
      	  var url = ctx+href;
      	  //var h = $(window).height();
      	  var content = '<iframe src="'+url+'" style="width:100%;height:100%;" frameborder="0" ></iframe>';
//      	  var content = '<iframe src="'+url+'" style="width:100%;height:'+h+'px;" frameborder="0" ></iframe>';
      	  element.tabAdd('iframe-tabs', {
      		  title: text
      		  ,content: content
      		  ,id: id
      	  })
        }
        element.tabChange('iframe-tabs', id); 
  	}
    
    
    /* 最大化窗口 */
    $(".eh-screen-full").on("click", function (e) {
        e.preventDefault();
        if (!$(this).hasClass("full-on")) {
            var docElm = document.documentElement;
            var full = docElm.requestFullScreen || docElm.webkitRequestFullScreen ||
                docElm.mozRequestFullScreen || docElm.msRequestFullscreen;
            "undefined" !== typeof full && full && full.call(docElm);
        } else {
            document.exitFullscreen ? document.exitFullscreen()
                : document.mozCancelFullScreen ? document.mozCancelFullScreen()
                : document.webkitCancelFullScreen ? document.webkitCancelFullScreen()
                    : document.msExitFullscreen && document.msExitFullscreen()
        }
        $(this).toggleClass("full-on");
    });

    /* 新建或切换标签栏 */
    var tabs = function (url) {
        var item = $('[lay-url="' + url + '"]');
        if (url !== undefined && url !== '#' && item.length > 0) {
            var bootLay = $('[lay-id="' + url + '"]');
            if (bootLay.length === 0) {
                var title = item.attr("lay-icon") === 'true' ? item.html()
                    : item.children(".layui-nav-title").text();
                var iframeUrl = (window.location.pathname + url).replace('//','/');
                element.tabAdd('iframe-tabs', {
                    title: title
                    , content: '<iframe src="' + iframeUrl + '" frameborder="0" class="layui-layout-iframe"></iframe>'
                    , id: url
                });
            }
            element.tabChange('iframe-tabs', url);
        }
    };

    /* 监听导航栏事件，实现标签页的切换 */
    element.on("nav(layui-nav-side)", function () {
        var url = $(this).attr('lay-url');
        tabs(url);
    });

    // 监听标签栏事件，实现导航栏高亮显示 
    element.on("tab(iframe-tabs)", function (e) {
    	//console.log(e)
        var layId = $(this).attr("lay-id");
        //console.log(layId)
        //功能正常，但是会报异常Maximum call stack size exceeded,浏览器会卡死
        //layui.hxNav('select', layId );
    });

    /* 监听hash来切换选项卡*/
    window.onhashchange = function (e) {
        var url = location.hash.replace(/^#/, '');
        var index = $(".layui-layout-admin .layui-side .layui-nav-item")[0];
        $(index).children("a").attr("lay-icon", "true");
        if (url === "" || url === undefined) {
            url = $(index).children("[lay-url]").attr("lay-url");
        }
        tabs(url);
    };
    window.onhashchange();

    
$("#userInfo").click(function(){
	var url = ctx+"/userInfo";
	$.get(url, {}, function(res){
		if(!isJSON(res)){
		  layer.open({
				type: 1,
				title: '个人信息',
				skin: 'layui-layer-rim', 
				area: ['50%', '57%'],  
		        content: res,
				btn: []
		  });
		}
	  });
})

$("#editPwd").click(function(){
	$("#editPwd-form")[0].reset();
    layui.form.render();
	layer.open({
		type: 1,
		skin: 'layui-layer-rim',
		area: ['20%', '35%'],  
		content: $('#editPwdView'),
		btn: []
	});
})

$("#editPwdSave").formSubmit({
	url : ctx + "/editPwd"
})

$("#logout").click(function(){
	window.location.href = ctx + "/logout";
})


//自动打开首页介绍
createTab("首页介绍",2081,ctx+"/sys/introduce/index");


});